package com.qq.client.view;

import com.qq.client.model.ManageQqChat;
import com.qq.common.Friend;
import com.qq.common.FriendType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author weiBin
 * @date 2019/1/8
 */
public class QqFriendList extends JFrame implements ActionListener, MouseListener {
    private String ownerNo;
    //卡片1
    JPanel jphy1, jphy2, jphy3;
    JButton jphy_jb1, jphy_jb2, jphy_jb3;
    JScrollPane jsp1;
    //卡片2
    JPanel jpmsr1, jpmsr2, jpmsr3;
    JButton jpmsr_jb1, jpmsr_jb2, jpmsr_jb3;
    JScrollPane jsp2;
    //卡片3
    JPanel jphmd1, jphmd2, jphmd3;
    JButton jphmd_jb1, jphmd_jb2, jphmd_jb3;
    JScrollPane jsp3;
    //好友列表
    JLabel[] jbls_hy, jbls_msr, jbls_hmd;
    CardLayout cl;
    List<Friend> hyList, msrList, hmdList;

    /**
     * 更新好友状态
     *
     * @param list
     */
    public void updateFriendList(List<Friend> list) {
        List<Friend> hyList = list.stream().filter(e -> FriendType.HY.equals(e.getType())).collect(Collectors.toList());
        List<Friend> msrList = list.stream().filter(e -> FriendType.MSR.equals(e.getType())).collect(Collectors.toList());
        List<Friend> hmdList = list.stream().filter(e -> FriendType.HMD.equals(e.getType())).collect(Collectors.toList());

    }

    /**
     * 加载好友列表
     *
     * @param list
     */
    public void loadFriendList(List<Friend> list) {
        List<Friend> hyList = list.stream().filter(e -> FriendType.HY.equals(e.getType())).collect(Collectors.toList());
        List<Friend> msrList = list.stream().filter(e -> FriendType.MSR.equals(e.getType())).collect(Collectors.toList());
        List<Friend> hmdList = list.stream().filter(e -> FriendType.HMD.equals(e.getType())).collect(Collectors.toList());
        jbls_hy = new JLabel[hyList.size()];
        jbls_msr = new JLabel[msrList.size()];
        jbls_hmd = new JLabel[hmdList.size()];
        for (int i = 0; i < hyList.size(); i++) {
            jbls_hy[i] = new JLabel(hyList.get(i).getNo(), new ImageIcon(QqFriendList.class.getResource("/image/mm.jpg")), JLabel.LEFT);
            jbls_hy[i].addMouseListener(this);
            if (("1").equals(hyList.get(i).getOnLine())) {
                jbls_hy[i].setEnabled(true);
            } else {
                jbls_hy[i].setEnabled(false);
            }
            jphy2.add(jbls_hy[i]);
        }
        for (int i = 0; i < msrList.size(); i++) {
            jbls_msr[i] = new JLabel(msrList.get(i).getNo(), new ImageIcon(QqFriendList.class.getResource("/image/mm.jpg")), JLabel.LEFT);
            jbls_msr[i].addMouseListener(this);
            jpmsr3.add(jbls_msr[i]);
        }
        for (int i = 0; i < hmdList.size(); i++) {
            jbls_hmd[i] = new JLabel(hmdList.get(i).getNo(), new ImageIcon(QqFriendList.class.getResource("/image/mm.jpg")), JLabel.LEFT);
            jbls_hmd[i].addMouseListener(this);
            jphmd3.add(jbls_hmd[i]);
        }
        this.hmdList = hmdList;
        this.hyList = hyList;
        this.hmdList = hmdList;
    }

    public QqFriendList(String ownerNo) {
        this.ownerNo = ownerNo;
        //======================卡片1 start=================================
        jphy_jb1 = new JButton("我的好友");
        jphy_jb2 = new JButton("陌生人");
        jphy_jb3 = new JButton("黑名单");
        jphy_jb2.addActionListener(this);
        jphy_jb3.addActionListener(this);
        jphy1 = new JPanel(new BorderLayout());
        jphy2 = new JPanel(new GridLayout(50, 1, 4, 4));
        jsp1 = new JScrollPane(jphy2);
        jphy3 = new JPanel(new GridLayout(2, 1));
        jphy3.add(jphy_jb2);
        jphy3.add(jphy_jb3);

        //添加北部组件
        jphy1.add(jphy_jb1, "North");
        //添加中部组件
        jphy1.add(jsp1, "Center");
        //添加南部组件
        jphy1.add(jphy3, "South");
        //======================卡片1 end===================================
        //======================卡片2 start=================================
        jpmsr_jb1 = new JButton("我的好友");
        jpmsr_jb1.addActionListener(this);
        jpmsr_jb2 = new JButton("陌生人");
        jpmsr_jb3 = new JButton("黑名单");
        jpmsr_jb3.addActionListener(this);
        jpmsr1 = new JPanel(new BorderLayout());
        jpmsr2 = new JPanel(new GridLayout(2, 1));
        jpmsr2.add(jpmsr_jb1);
        jpmsr2.add(jpmsr_jb2);
        jpmsr3 = new JPanel(new GridLayout(20, 1, 4, 4));
        jsp2 = new JScrollPane(jpmsr3);
        //添加北部组件
        jpmsr1.add(jpmsr2, "North");
        //添加中部组件
        jpmsr1.add(jsp2, "Center");
        //添加南部组件
        jpmsr1.add(jpmsr_jb3, "South");
        //======================卡片2 end===================================
        //======================卡片3 start=================================
        jphmd_jb1 = new JButton("我的好友");
        jphmd_jb1.addActionListener(this);
        jphmd_jb2 = new JButton("陌生人");
        jphmd_jb2.addActionListener(this);
        jphmd_jb3 = new JButton("黑名单");
        jphmd1 = new JPanel(new BorderLayout());
        jphmd2 = new JPanel(new GridLayout(3, 1));
        jphmd2.add(jphmd_jb1);
        jphmd2.add(jphmd_jb2);
        jphmd2.add(jphmd_jb3);
        jphmd3 = new JPanel(new GridLayout(10, 1, 4, 4));
        jsp3 = new JScrollPane(jphmd3);
        jphmd1.add(jphmd2, "North");
        jphmd1.add(jsp3, "Center");
        //======================卡片3 end===================================
        cl = new CardLayout();
        this.setLayout(cl);
        //添加卡片1
        this.add(jphy1, "1");
        //添加卡片2
        this.add(jpmsr1, "2");
        //添加卡片3
        this.add(jphmd1, "3");
        this.setSize(200, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle(ownerNo);
    }

    public static void main(String[] args) {
        QqFriendList qqFriendList = new QqFriendList("500000");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 如果点击了陌生人按钮
        if (e.getSource() == jphy_jb2 || e.getSource() == jphmd_jb2) {
            cl.show(this.getContentPane(), "2");
            //点击了我的好友
        } else if (e.getSource() == jpmsr_jb1 || e.getSource() == jphmd_jb1) {
            cl.show(this.getContentPane(), "1");
            //点击了黑名单
        } else if (e.getSource() == jpmsr_jb3 || e.getSource() == jphy_jb3) {
            cl.show(this.getContentPane(), "3");
        }
    }

    /**
     * 鼠标点击时
     *
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        //双击得到好友编号
        if (e.getClickCount() == 2 && ((JLabel) e.getSource()).isEnabled()) {
            String friendNo = ((JLabel) e.getSource()).getText();
            System.out.println("你希望和 " + friendNo + " 聊天");
            QqChat qqChat = new QqChat(ownerNo, friendNo);
            ManageQqChat.setQqChat(ownerNo+"_"+friendNo,qqChat);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    /**
     * 鼠标移入时
     *
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        JLabel jLabel = (JLabel) e.getSource();
        jLabel.setForeground(Color.RED);
    }

    /**
     * 鼠标移开时
     *
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {
        JLabel jLabel = (JLabel) e.getSource();
        jLabel.setForeground(Color.BLACK);
    }
}
